﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkManagement;

namespace LorraineSec.MessageLib.Messages
{
    public sealed class LogListRequest : EncryptedMessage
    {
        #region Format Constants
        /*
         * Format:
         *   4         4          4       4          1             x
         *|LogID|CompartmentID|AssetID|UserID|DeviceSerialLen|DeviceSerial| 
         */

        //Lengths
        private const int ENC_LOGID_LENGTH = 4;
        private const int ENC_COMPARTMENTID_LENGTH = 4;
        private const int ENC_ASSETID_LENGTH = 4;
        private const int ENC_USERID_LENGTH = 4;
        private const int ENC_DEVICESERIALLEN_LENGTH = 1;

        //Offsets
        private const int ENC_LOGID_OFFSET = 0;
        private const int ENC_COMPARTMENTID_OFFSET = ENC_LOGID_OFFSET + ENC_LOGID_LENGTH;
        private const int ENC_ASSETID_OFFSET = ENC_COMPARTMENTID_OFFSET + ENC_COMPARTMENTID_LENGTH;
        private const int ENC_USERID_OFFSET = ENC_ASSETID_OFFSET + ENC_ASSETID_LENGTH;
        private const int ENC_DEVICESERIALLEN_OFFSET = ENC_USERID_OFFSET + ENC_USERID_LENGTH;
        private const int ENC_DEVICESERIAL_OFFSET = ENC_DEVICESERIALLEN_OFFSET + ENC_DEVICESERIALLEN_LENGTH;

        //Known Length
        private const int ENC_KNOWN_LENGTH = ENC_LOGID_LENGTH + ENC_COMPARTMENTID_LENGTH + ENC_ASSETID_LENGTH + ENC_USERID_LENGTH + ENC_DEVICESERIALLEN_LENGTH;
        
        #endregion

        public uint LogID;
        public uint CompartmentID;
        public uint AssetID;
        public uint UserID;
        public string DeviceSerial;

         public LogListRequest()
        {
            init();
        }

        public LogListRequest(byte[] bytes)
            : base(bytes)
        {
            init();
            decode();
        }

        private void init()
        {
            MessageID = (ushort)LorraineSecMessageFactory.MessageID.LOG_LIST_REQUEST;
        }

        public override void decrypt(CryptoManager crypt)
        {
            //Decrypt byte array
            base.decrypt(crypt);
            //Decode byte array
            LogID = Bytes.getUInt32(encData, ENC_LOGID_OFFSET);
            CompartmentID = Bytes.getUInt32(encData, ENC_COMPARTMENTID_OFFSET);
            AssetID = Bytes.getByte(encData, ENC_ASSETID_OFFSET); 
            UserID = Bytes.getUInt32(encData, ENC_USERID_OFFSET);
            DeviceSerial= Bytes.getString(encData, ENC_DEVICESERIAL_OFFSET, Bytes.getByte(encData, ENC_DEVICESERIALLEN_OFFSET));
        }

        public override void encrypt(CryptoManager crypt)
        {
            //Encode Byte Array
            encData = new byte[ENC_KNOWN_LENGTH + DeviceSerial.Length];
            Bytes.setValue(encData, ENC_LOGID_OFFSET, LogID);
            Bytes.setValue(encData, ENC_COMPARTMENTID_OFFSET, CompartmentID);
            Bytes.setValue(encData, ENC_ASSETID_OFFSET, AssetID);
            Bytes.setValue(encData, ENC_USERID_OFFSET, UserID);
            Bytes.setValue(encData, ENC_DEVICESERIALLEN_OFFSET, (byte) DeviceSerial.Length);
            Bytes.setValue(encData, ENC_DEVICESERIAL_OFFSET + DeviceSerial.Length, DeviceSerial);
            //Encrypt Byte Array
            base.encrypt(crypt);
        }

        
    }
}
